﻿using UnityEditor;
namespace UtilEditors
{
    using System; //引用命名空间下
    using System.Runtime.InteropServices;//调用外部库，需要引用命名空间
    using UnityEngine;
    using UnityEngine.UI;


    /// <summary>
    /// 为了调用外部库脚本
    /// </summary>
    public class ChinarMessage
    {
        [DllImport("User32.dll",SetLastError = true,ThrowOnUnmappableChar = true,CharSet = CharSet.Auto)]
        public static extern int MessageBox(IntPtr handle,String message,String title,int type);//具体方法
    }


    //微软弹窗
    public class PopoutWindows
    {
        private static int returnNumber; //返回值 
        /// <summary>
        /// 9个按钮对应弹框
        /// </summary>
        /// <param name="index"></param>
        public static int ShowWindow(string title,string com,int index = 0)
        {
            returnNumber = ChinarMessage.MessageBox(IntPtr.Zero,com,title,index);
            //switch(index)
            //{
            //    case 0: 
            ////                                                                     0   Chinar-0:返回值均：1
            //        break;
            //    case 1:
            //        returnNumber = ChinarMessage.MessageBox(IntPtr.Zero,com,title,1);//Chinar-1:确认：1，取消：2
            //
            //        break;
            //    case 2:
            //        returnNumber = ChinarMessage.MessageBox(IntPtr.Zero,com,title,2);//"Chinar-2:中止：3，重试：4，忽略：5"
            //
            //        break;
            //    case 3:
            //        returnNumber = ChinarMessage.MessageBox(IntPtr.Zero,com,title,3);//"Chinar-3:是：6，否：7，取消：2"
            //
            //        break;
            //    case 4:
            //        returnNumber = ChinarMessage.MessageBox(IntPtr.Zero,com,title,4);//"Chinar-4:是：6，否：7"
            //
            //        break;
            //    case 5:
            //        returnNumber = ChinarMessage.MessageBox(IntPtr.Zero,com,title,5);//"Chinar-5:重试：4，取消：2"
            //
            //        break;
            //    case 6:
            //        returnNumber = ChinarMessage.MessageBox(IntPtr.Zero,com,title,6);//"Chinar-6:取消：2，重试：10，继续：11"
            //
            //        break;
            //}
            return returnNumber;
        }


    }
    /// <summary>
    /// 弹框提示
    /// </summary>
    public class Popout :EditorWindow
    {
        public static Popout window;
        private static string customTxt;
        private static Action[] action;
        static int Num = 0;
        public static void ShowWindow(string _title,string message,params Action[] act)
        {
            if(window == null)
            {
                window = (Popout)GetWindow(typeof(Popout));
            }
            action = act;
            if(action != null)
            {
                Num = action.Length;
            }
            window.titleContent.text = _title;
            customTxt = message;
            window.position = new Rect(1920 / 2 - 150,1080 / 2 - 50,300,100);
            window.Show();
        }

        void OnGUI()
        {
            GUILayout.FlexibleSpace();
            {
                GUILayout.Label(customTxt);
            }
            GUILayout.FlexibleSpace();
            GUILayout.BeginHorizontal();
            {
                GUILayout.FlexibleSpace();
                {
                    if(GUILayout.Button("确定",GUILayout.Width(80),GUILayout.Height(40)))
                    {
                        window.Close();
                        if(Num > 0)
                        {
                            action[0].Invoke();
                        }
                    }

                    if(Num > 1)
                    {
                        if(GUILayout.Button("取消",GUILayout.Width(80),GUILayout.Height(40)))
                        {
                            window.Close();
                            action[1].Invoke();
                        }
                    }
                    if(Num > 2)
                    {
                        if(GUILayout.Button("停止",GUILayout.Width(80),GUILayout.Height(40)))
                        {
                            window.Close();
                            action[2].Invoke();
                        }
                    }
                }
                GUILayout.FlexibleSpace();
            }
            GUILayout.EndHorizontal();

        }

        public static void WindowHint(string message,string title = "提示",bool isPause = true,params Action[] action)
        {
            int index = action.Length;
            switch(index)
            {
                case 0:
                    if(isPause)
                    {
                        var isV = EditorUtility.DisplayDialog(title,message,"确认","取消");
                        if(isV)
                        {
                            if(action.Length > 0)
                            {
                                if(action[0] != null)
                                {
                                    action[0].Invoke();
                                }
                            }
                        }
                        else
                        {
                            if(action.Length > 1)
                                if(action[1] != null)
                                    action[1].Invoke();
                        }
                    }
                    else
                    {
                        Popout.ShowWindow(title,message,action);
                    }


                    break;
                case 1:
                    if(isPause)
                    {
                        var isVs = EditorUtility.DisplayDialog(title,message,"确认","取消");
                        if(isVs)
                        {
                            if(action.Length > 0)
                            {
                                if(action[0] != null)
                                {
                                    action[0].Invoke();
                                }
                            }
                        }
                        else
                        {
                            if(action.Length > 1)
                                if(action[1] != null)
                                    action[1].Invoke();
                        }
                    }
                    else
                    {
                        Popout.ShowWindow(title,message,action);
                    }
                    break;

                case 2:
                    if(isPause)
                    {
                        int option = EditorUtility.DisplayDialogComplex(title,message,"确认","取消","停止");
                        if(option == 0)
                        {
                            if(action[0] != null)
                                action[0].Invoke();
                        }
                        else if(option == 1)
                        {
                            if(action[1] != null)
                                action[1].Invoke();
                        }
                        else if(option == 2)
                        {
                            if(action[2] != null)
                                action[2].Invoke();
                        }
                    }
                    else
                    {
                        Popout.ShowWindow(title,message,action);
                    }
                    break;
            }
        }
    }
}